<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
$return['setting']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
	$id = intval($_RQ['id']);
	if(!$id){
		exi('没有找到该信息，请重新选择！','error');
	}
	$item=pdo_get('jy_zhaopin_job',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$item['images']=$item['images']?json_decode($item['images'],true):array();
	$pidcode='red_'.$item['type'].'_'.$id;
	$item['isgetred']=model('core/red_pack')->check_getred($pidcode,'jy_zhaopin');
	$member = pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('createtime','credit1','credit2','mobile','avatar','nickname'));
	$merber['isvip']=pdo_getcolumn('jy_zhaopin_merch',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['id']),'isvip');
	//添加一次浏览次数
	pdo_update('jy_zhaopin_job',array('looks +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	//进入作品详情，记录访问者浏览信息
	$islook=pdo_get('jy_zhaopin_looklog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'itemid'=>$id));
	if(empty($islook)){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'itemid'=>$id,
			'mid'=>$_SESSION['uid'],
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_zhaopin_looklog',$insert);
	}else{
		pdo_update('jy_zhaopin_looklog',array('createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'itemid'=>$id));
	}
	$item['msgnum']=pdo_count('jy_zhaopin_comment',array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'itemid'=>$item['id']),'id');
	//按点赞数排序作品
	$pindex = max(1, intval($_RQ['page']));
	$psize = 30;
	$item['msg']=pdo_getslice('jy_zhaopin_comment',array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'itemid'=>$item['id']),array($pindex,$psize),$total,$fields,'','createtime desc');
	if($item['msg']){
		foreach($item['msg'] as &$m){
			$m['createtime'] = date('Y-m-d H:i',$m['createtime']);
			$m['floorid'] = '';
			$m['time'] = date('m/d',$m['createtime']);
			$m['member'] = pdo_get('core_members',array('id'=>$m['mid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','avatar','nickname'));
		}
	}
	$return['isme']=$item['mid']==$_SESSION['uid'] ? 1 :0;
	$item['createtime']=$item['createtime']?date('Y-m-d H:i:s',$item['createtime']):'';
	$item['content'] = json_decode($item['content'],true);
	$item['diyfileds'] = json_decode($item['diyfileds'],true);
	$item['fee']=0;
	if($item['diyfileds']){
		foreach($item['diyfileds'] as $k=>&$fv){
			if($fv['type']==5){
				$fv['data']=explode(';',$fv['data']);
				foreach($fv['data'] as $ki=>$vv){
					if(!$vv){
						unset($fv['data'][$ki]);
					}
				}
			}
		}
	}
	if($item['content']){
		foreach($item['content'] as &$row){
			if($row['type']==1){
				$item['desc'] = $row['data'];
			}
		}
	}
	$item['desc'] = $item['desc']?$item['desc']:$user['desc'];
	$this->session('id',$_RQ['id']);
	$return['sex']=array('1'=>'男','2'=>'女');
	$return['educationid']=array('1'=>'初中及以下','2'=>'高中、中专','3'=>'大专','4'=>'本科','5'=>'研究生及以上');
	$return['timesid']=array('1'=>'一年以下','2'=>'一至三年','3'=>'三到五年','4'=>'五年以上');
	$return['classifyid']=array('1'=>'电脑办公','2'=>'家具家电','3'=>'手机','4'=>'文体户外','5'=>'服饰配饰','6'=>'儿童母婴','7'=>'美容保健','8'=>'数码产品','9'=>'居家日常','10'=>'其他');
	$return['isbuy']=array('0'=>'求购','1'=>'出售','3'=>'转让');
	$return['lev']=array('1'=>'轿车','2'=>'SUV','3'=>'MPV','4'=>'微面','5'=>'皮卡','6'=>'电动车','7'=>'其他');
	$return['pay']=array('0'=>'面议','1'=>'1-1000','2'=>'1000-2000','3'=>'2000-3000','4'=>'3000-5000','5'=>'5000-8000','6'=>'8000-10000','7'=>'1000-15000','8'=>'15000以上');
	$item['sex']=$return['sex'][$item['sex']];
	$item['educationid']=$return['educationid'][$item['educationid']];
	$item['timesid']=$return['timesid'][$item['timesid']];
	$item['classifyid']=$return['classifyid'][$item['classifyid']];
	$item['isbuy']=$return['isbuy'][$item['isbuy']];
	$item['lev']=$return['lev'][$item['lev']];
	$item['pay']=$return['pay'][$item['pay']];
	$lat_lng=$this->session('lat_lng');	
	$item['lat_lng']=$item['lat_lng']?json_decode($item['lat_lng'],true):array('lat'=>'','lng'=>'');
	$item['lat_lng']=$item['lat_lng']=='null'?array():$item['lat_lng'];
	if($item['lat_lng']['lng'] && $item['lat_lng']['lat']){
		$item['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$item['lat_lng']['lng'],$item['lat_lng']['lat']);
		$item['juli']=intval($item['juli']/1000);
	}
	$return['merch']=pdo_get('jy_merch_user',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
	$return['user']=$user;
	$return['member']=$member;
	$return['item']=$item;
	$return['islike']=$islook['islike'];
	$return['sharedata']=array(
		'title'=>$item['title'],
		'desc'=>$item['remark'],
		'link'=>custom_url('jy_zhaopin/item_detail//id='.$item['id'].'&fromuser='.$_SESSION['uid'],true),
		'imgUrl'=>tomedia($item['images'][0]),
	);
	set_html('title',$item['title']);
	exi($return);
}
if($_RQ['op']=='follow'){
	$id = $this->session('id');
	if(!$id){
		exi('没有找到该作品，请重新选择！','error');
	}
	$item=pdo_get('jy_zhaopin_job',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$follow=pdo_get('jy_zhaopin_userlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$item['mid']));
	if($follow){
		if($follow['isfollow'] !=1){
			pdo_update('jy_zhaopin_userlog',array('isfollow'=>1,'createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$item['mid']));
			
		}else{
			pdo_update('jy_zhaopin_userlog',array('isfollow'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$item['mid']));
		}
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'userid'=>$item['mid'],
			'mid'=>$_SESSION['uid'],
			'isfollow'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_zhaopin_userlog',$insert);
	}
	exi('关注成功');
}
if($_RQ['op']=='pingxuan'){
	$id = $this->session('id');
	$postdata=get_postdata();
	$index = intval($postdata['index']);
	if(!$id){
		exi('没有找到该作品，请重新选择！','error');
	}
	$item=pdo_get('jy_zhaopin_job',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$item['fileds'] = json_decode($item['fileds'],true);
	$fileds=pdo_getcolumn('jy_zhaopin_job_jieli',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id),'fileds');
	if($fileds){
		$fileds=json_decode($fileds,true);
		if($fileds[$index]['selected']==1){
			$fileds[$index]['selected']=0;
			$item['fileds'][$index]['nums'] -=1;
		}else{
			$fileds[$index]['selected']=1;
			$item['fileds'][$index]['nums'] +=1;
		}		
		pdo_update('jy_zhaopin_job_jieli',array('fileds'=>ijson_encode($fileds)),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
	}else{
		$fileds = $item['fileds'];
		$fileds[$index]['selected']=1;
		$insert=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$_SESSION['uid'],
					'pid'=>$id,
					'snsid'=>$item['snsid'],
					'type'=>$item['type'],
					'orderid'=>0,
					'fileds'=>ijson_encode($item['fileds']),
					'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_zhaopin_job_jieli',$insert);
		$item['fileds'][$index]['nums'] +=1;
	}
	pdo_update('jy_zhaopin_job',array('fileds'=>ijson_encode($item['fileds'])),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi('接龙成功');
}
if($_RQ['op']=='shoucang'){
	$id = $this->session('id');
	if(!$id){
		exi('没有找到该作品，请重新选择！','error');
	}
	$item=pdo_get('jy_zhaopin_job',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$shoucang=pdo_get('jy_zhaopin_shoucang',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
	if($shoucang){
		if($shoucang['isshoucang'] !=1){
			pdo_update('jy_zhaopin_shoucang',array('isshoucang'=>1,'createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
			exi('收藏成功');
		}else{
			pdo_update('jy_zhaopin_shoucang',array('isshoucang'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
		   exi('取消收藏');
		}
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'pid'=>$id,
			'mid'=>$_SESSION['uid'],
			'isshoucang'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_zhaopin_shoucang',$insert);
	}
	exi('收藏成功');
}
if($_RQ['op']=='setmsg'){
	$id = $this->session('id');
	$item=pdo_get('jy_zhaopin_job',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$postdata=get_postdata();
	$data=array(
		'comment'=>trim($postdata['msg']),
		'mid'=>$_SESSION['uid'],
		'itemid'=>$id,
		//'cid'=>intval($postdata['cid']),
		'uniacid'=>$_SESSION['uniacid'],
		'item_mid'=>$item['mid'],
		'typeid'=>$item['type'],
		'status'=>1,
		'createtime'=>time(),
	);
	$msg='评论成功！';
	if($basic['msg_shenhe']){
		$data['status']=0;
		$msg='评论成功,等待审核！';
	}
	pdo_insert('jy_zhaopin_comment',$data);
	exi($msg);
}
if ($_RQ['op'] == 'pregetred') {
	$resultJson = [];
  	$postdata = get_postdata();
	$pid = intval($postdata['pid']);
	$item=pdo_get('jy_zhaopin_job',array('id'=>$pid,'uniacid'=>$_SESSION['uniacid']),array('title','mid','redmoney','rednum','type'));	
	$pidcode='red_'.$item['type'].'_'.$pid;
	$isgetred=model('core/red_pack')->check_getred($pidcode,'jy_zhaopin');
	if($isgetred){
		exi('您已抢过该红包！','error');
	}
	$params=array(
		'mid'=>$_SESSION['uid'],
		'uniacid'=>$_SESSION['uniacid'],
		'pidcode'=>$pidcode,
		'plugin'=>'jy_zhaopin',
	);
	$psize = 20;
	$pindex = max(1, intval($_RQ['page']));
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'fields'=>$fields,
		'orderby'=>'id desc',
	);
	$red=model('core/red_pack')->getall($parameter);
  	$sender = pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']), array('nickname', 'avatar'));  
	$nickname  = $sender['nickname'];
  	$send_name = $send_name?$send_name:$nickname;
  	$send_avatar = $member['logo'] ? $member['logo'] : $sender['avatar'];
  	$send_avatar = $send_avatar ? $send_avatar :  '../core/resource/images/nopic.jpg';
	$parameter['sum']='money';
	$issendred=model('core/red_pack')->getsum($parameter);;
	$issendnum=count($red);
	$yufee=$item['redmoney']-$issendred;
	$yutotal=$item['rednum']-$issendnum;
	if($yutotal<1 && $yufee<=0){
		exi($item['redmoney'].'噢！红包已抢完，下次再来吧！','error');
	}
  	$resultJson = array_merge($resultJson, [
            'red_sender_name' => $send_name,
            'red_sender_avatar' => $send_avatar,
            'red_title' => $item['title']
    ]);
    exi($resultJson);
}
if($_RQ['op']=='like'){
	$id = $this->session('id');
	$postdata=get_postdata();
	if(!$id){
		exi('没有找到该作品，请重新选择！','error');
	}
	$looklist=pdo_get('jy_zhaopin_job_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
	if($looklist){
		if($looklist['islike'] !=1){
			pdo_update('jy_zhaopin_job',array('like +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			pdo_update('jy_zhaopin_job_like',array('islike'=>1),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
			
		}elseif($postdata['like'] !=1){
			pdo_update('jy_zhaopin_job',array('like -='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			pdo_update('jy_zhaopin_job_like',array('islike'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
		}
	}
	exi('点赞成功');
}
if($_RQ['op']=='msglike'){
	$id = $this->session('id');
	$postdata=get_postdata();
	$cid = intval($postdata['cid']);
	if(!$id){
		exi('没有找到该作品，请重新选择！','error');
	}
	$params = array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id,'cid'=>$cid);
	$msglike=pdo_get('jy_zhaopin_job_msglike',$params);
	if($msglike){
		if($msglike['islike'] !=1){
			pdo_update('jy_zhaopin_job_msglike',array('islike'=>1),$params);
			
		}elseif($postdata['like'] !=1){
			pdo_update('jy_zhaopin_job_msglike',array('islike'=>0),$params);
		}
	}else{
		$data=array(
			'mid'=>$_SESSION['uid'],
			'pid'=>$id,
			'cid'=>$cid,
			'uniacid'=>$_SESSION['uniacid'],
			'islike'=>1,
		);
		pdo_insert('jy_zhaopin_job_msglike',$data);
	}
	exi('点赞成功');
}
if($_RQ['op']=='readjieli'){
	$id = $this->session('id');
	$item=pdo_get('jy_zhaopin_job',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if(!$id){
		exi('没有找到该帖子，请重新选择！','error');
	}
	$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'pid'=>$id,
				'snsid'=>$item['snsid'],
				'type'=>$item['type'],
				'orderid'=>0,
				'createtime'=>TIMESTAMP
	);
	pdo_insert('jy_zhaopin_job_jieli',$insert);
	exi('接龙成功');
}
if($_RQ['op']=='setfileds'){
	$id = $this->session('id');
	$item=pdo_get('jy_zhaopin_job',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$fileds = json_decode($item['fileds'],true);
	foreach($fileds as &$pv){
		$pv['total']=1;
	}
	$postdata=get_postdata();
	$type = intval($postdata['type']);
	$index = intval($postdata['index']);
	if($type){
		$fileds[$index]['total'] +=1;
	}else{
		$fileds[$index]['total'] -=1;
	}
	$fileds = ijson_encode($fileds);
	$this->session('fileds',$fileds);
	exi('设置成功！');
}
if($_RQ['op']=='sendhb'){
	$id=$_RQ['id'];
	if(!$_RQ['id']){
		$id=$this->session('id');
	}
	if(!$id){
		exi('未正确获取作品id','error');
	}
	
	$post=array();
	$post['money']=0;
	 $user=pdo_get('jy_zhaopin_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	 $item=pdo_get('jy_zhaopin_job',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($_RQ['postdata']){
	   $postdata=get_postdata();
	   $money=$postdata['money'];
	   $username=$postdata['username'];
	   if($money<=0){
		   exi('红包金额需大于0元','error');
	   }
	   if($money>200){
		   exi('单个红包金额最多不超过200元','error');
	   }
	   if(!$username){
		   exi('请输入发送人姓名！','error');
	   }
	   //创建红包订单
	   $insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'pid'=>$id,
			'omid'=>$item['mid'],
			'fee'=>$money,
			'username'=>$username,
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP
	   );
	   pdo_insert('jy_zhaopin_redorder',$insert);
	   //支付
	   $id=pdo_insertid();
	   $params['fee']=$money;
	    $params['title']='发送红包订单';
		$params['id']=$id;
		$params['plugin']='jy_zhaopin';
		$params['back_url']='jy_zhaopin/item_detail/payresult';
		cfc('core')->pay($params);
	}
	$return['post']=$post;
	exi($return);
}

if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['payresult'];unset($_SESSION['payresult']);
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_zhaopin_redlog',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！发送红包订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！发送红包订单金额不匹配！','error');
	}
	pdo_update('jy_zhaopin_redorder',array('ispay'=>1,'fee'=>$payresult['fee']),array('id'=>$order['id']));
	//发红包
	//收红包人的信息
	$suser=pdo_get('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['omid']));
	$arr['openid'] = $suser['openid'];
	$arr['hbname'] = '场景投票';
	$arr['body'] = "场景投票红包";
	$arr['fee'] = $order['fee'];
	$arr['openid'] = $fans['openid'];
	$arr['mchid'] = $basic['mchid'];
	$arr['mch_appid'] = $basic['mch_appid'];
	$arr['username'] = $order['username'];
	$res = sendhongbaoto($arr);
 	if($res['result_code'] == 'SUCCESS'){
		pdo_update('jy_zhaopin_redorder',array('status'=>1),array('id'=>$order['id']));
		exi('支付成功','success');
	}
	else{
		exi('红包发送失败','error');
	}
}
if($_RQ['op']=='getred'){
	$pid = intval($_RQ['pid']);
	$item=pdo_get('jy_zhaopin_job',array('id'=>$pid,'uniacid'=>$_SESSION['uniacid']),array('title','desc','mid','rednum','snsid','type'));
	$red=pdo_get('jy_zhaopin_redlog',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid'=>0,'status'=>1),array('fee','setmid'));
	$send_name = pdo_getcolumn('jy_zhaopin_member',array('mid'=>$red['setmid'],'uniacid'=>$_SESSION['uniacid']),'title');
	$nickname  = pdo_getcolumn('core_members',array('id'=>$red['setmid'],'uniacid'=>$_SESSION['uniacid']),'nickname');
	$send_name = $send_name?$send_name:$nickname;
	$params =array(
			'openid'=>$_SESSION['openid'],
			'money'=>$red['fee'],
			'send_name'=>$send_name,
			'total_num'=>$item['rednum'],
			'wishing'=>$item['desc'],
			'act_name'=>$item['title'],
	);
	$res = cfc('wechat_pay')->grouppack($params);
	if($res['result_code'] == 'SUCCESS'){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'pid'=>$pid,
			'setmid'=>$red['setmid'],
			'fee'=>$red['fee'],
			'status'=>1,
			'createtime'=>TIMESTAMP
	   );
	   pdo_insert('jy_zhaopin_redlog',$insert);
	   $orderid = pdo_insertid();
	   $insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'pid'=>$pid,
				'snsid'=>$item['snsid'],
				'type'=>$item['type'],
				'orderid'=>$orderid,
				'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_zhaopin_job_jieli',$insert);
	   exi('抢红包成功','success');
	}else{
		exi('红包获取失败','error');
	}
}